﻿using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;


public class QRCodeHelper
{
    public static MemoryStream GetQRCode(string content, int moduleSize = 9)
    {
        //ErrorCorrectionLevel 误差校正水平
        //QuietZoneModules     空白区域

        var encoder = new QrEncoder(ErrorCorrectionLevel.M);
        QrCode qrCode = encoder.Encode(content);
        GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White);

        MemoryStream memoryStream = new MemoryStream();
        render.WriteToStream(qrCode.Matrix, ImageFormat.Jpeg, memoryStream);

        return memoryStream;

        //生成图片的代码
        //DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
        //Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
        //Graphics g = Graphics.FromImage(map);
        //render.Draw(g, qrCode.Matrix);
        //map.Save(fileName, ImageFormat.Jpeg);//fileName为存放的图片路径
    }
    public static MemoryStream GetQRCode(string content, string iconPath, int moduleSize = 9)
    {
        QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
        QrCode qrCode = qrEncoder.Encode(content);

        GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White);

        DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
        Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
        Graphics g = Graphics.FromImage(map);
        render.Draw(g, qrCode.Matrix);

        //追加Logo图片 ,注意控制Logo图片大小和二维码大小的比例
        //PS:追加的图片过大超过二维码的容错率会导致信息丢失,无法被识别
        Image img = Image.FromFile(iconPath);

        Point imgPoint = new Point((map.Width - img.Width) / 2, (map.Height - img.Height) / 2);
        g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height);

        MemoryStream memoryStream = new MemoryStream();
        map.Save(memoryStream, ImageFormat.Jpeg);

        return memoryStream;

        //生成图片的代码： map.Save(fileName, ImageFormat.Jpeg);//fileName为存放的图片路径
    }

}
